package com.controller.jkt;

import com.entity.*;
import com.service.jkt.QiTaService;
import com.service.jkt.YeWuServise;
import com.util.gzp.AjaxResponse;
import com.util.gzp.file.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@RestController
@Api(tags = "业务的接口")
@RequestMapping("/yewu")
@Validated
public class YeWuController {

    @Resource
    private YeWuServise yeWuServiseservise;

    @GetMapping("/findshouli")
    @ApiOperation(value = "查询 受理 业务的方法，返回AjaxResponse", response = Map.class)
    public AjaxResponse findshouli() {
        Service service = new Service();
        //受理业务
        service.setServicestatusid(1);
        return yeWuServiseservise.ShowYeWu(service,1,90);
    }
    @GetMapping("/findruku")
    @ApiOperation(value = "查询 入库 业务的方法，返回AjaxResponse", response = Map.class)
    public AjaxResponse findruku() {
        Service service = new Service();
        //受理业务
        service.setServicestatusid(2);
        return yeWuServiseservise.ShowYeWu(service,1,90);
    }

    @GetMapping("/findshenpi")
    @ApiOperation(value = "查询 审批 业务的方法，返回AjaxResponse", response = Map.class)
    public AjaxResponse findshenpi() {
        Service service = new Service();
        //受理业务
        service.setServicestatusid(6);
        return yeWuServiseservise.ShowYeWu(service,1,90);
    }
    @GetMapping("/findshanzheng")
    @ApiOperation(value = "查询 缮证 业务的方法，返回AjaxResponse", response = Map.class)
    public AjaxResponse findshanzheng() {
        Service service = new Service();
        //受理业务
        service.setServicestatusid(3);
        return yeWuServiseservise.ShowYeWu(service,1,90);
    }


    @PostMapping("/yy")
    @ApiOperation(value = "添加 异议 业务的方法", response = Map.class)
    public AjaxResponse yy(
            @ApiParam("业务id") Integer serviceid,
            @ApiParam("客户id 同时也是 申请人id") Integer clientid,
            @ApiParam("异议事项") String dissentingitem,
            @ApiParam("房屋number")String roomnumber,
            @ApiParam("异议id")Integer id,
            // @ApiParam("登记申请书") File application,
            @ApiParam("登记申请书存储地址") String applicationpath,
            // @ApiParam("身份证复印件") File idcopy,
            @ApiParam("身份证复印件存储地址") String idcopypath,
            // @ApiParam("一寸照片") File photo,
            @ApiParam("一寸照片存储地址") String photopath,
            //@ApiParam("购房发票复印件") File purchasecopy,
            @ApiParam("购房发票复印件存储地址") String purchasecopypath,
            //@ApiParam("购房合同复印件") File contractcopy,
            @ApiParam("购房合同复印件存储地址") String contractcopypath,
            //@ApiParam("税证复印件") File taxcopy,
            @ApiParam("税证复印件存储地址") String taxcopypath
    ){
        //异议数据封装成对象
        Dissentingregistration dis = new Dissentingregistration();
        dis.setId(id);
        dis.setServiceid(serviceid);//业务宗号 （业务主键id）
        dis.setClientid(clientid);//申请人id
        dis.setDissentingitem(dissentingitem);//异议事项
        dis.setRegistertime(new Date());//登记时间，获取当前时间
        dis.setDescript(roomnumber);//附记用于存放roomnumber
        //业务数据封装成对象
        Service ser =new Service();
        ser.setServiceid(serviceid);
        //文件上传保存
        try {
            // FileUtil.uploadFile(application.toString().getBytes(),applicationpath,serviceid+"sqs");
            ser.setApplication(applicationpath+serviceid+"sqs");
            // FileUtil.uploadFile(idcopy.toString().getBytes(),idcopypath,serviceid+"sfz");
            ser.setIdcopy(idcopypath+serviceid+"sfz");
            // FileUtil.uploadFile(photo.toString().getBytes(),photopath,serviceid+"zp");
            ser.setPhoto(photopath+serviceid+"zp");
            //   FileUtil.uploadFile(purchasecopy.toString().getBytes(),purchasecopypath,serviceid+"fp");
            ser.setPurchasecopy(purchasecopypath+serviceid+"fp");
            //  FileUtil.uploadFile(contractcopy.toString().getBytes(),contractcopypath,serviceid+"ht");
            ser.setContractcopy(contractcopypath+serviceid+"ht");
            //   FileUtil.uploadFile(taxcopy.toString().getBytes(),taxcopypath,serviceid+"sz");
            ser.setTaxcopy(taxcopypath+serviceid+"sz");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return yeWuServiseservise.yyLuRu(dis,ser);
    }


    @PostMapping("/syq")
    @ApiOperation(value = "添加 所有权 业务的方法", response = Map.class)
    public AjaxResponse syq(
            @ApiParam("业务id") Integer serviceid,
            @ApiParam("业务编号") String servicenumber,
            @ApiParam("房屋id") Integer roomid,
            @ApiParam("所有权id") Integer osid,
            @ApiParam("共有人id")Integer clientid,
            @ApiParam("共有人份额")Integer share,
            @ApiParam("公有方式")Integer sharetypeid,

 //           @ApiParam("门牌号") String roomnumber,
//            @ApiParam("房屋状态")Integer roomstatusid,
//            @ApiParam("所属楼栋id")Integer buildingid,
//            @ApiParam("楼栋名称")String buildingname,
//            @ApiParam("楼栋编号")String buildingnumber,
//            @ApiParam("楼层")Integer floor,
//            @ApiParam("分户房屋")Integer count,
//            @ApiParam("总面积")Integer totalarea,
           // @ApiParam("登记申请书") File application,
            @ApiParam("登记申请书存储地址") String applicationpath,
           // @ApiParam("身份证复印件") File idcopy,
            @ApiParam("身份证复印件存储地址") String idcopypath,
           // @ApiParam("一寸照片") File photo,
            @ApiParam("一寸照片存储地址") String photopath,
            //@ApiParam("购房发票复印件") File purchasecopy,
            @ApiParam("购房发票复印件存储地址") String purchasecopypath,
            //@ApiParam("购房合同复印件") File contractcopy,
            @ApiParam("购房合同复印件存储地址") String contractcopypath,
            //@ApiParam("税证复印件") File taxcopy,
            @ApiParam("税证复印件存储地址") String taxcopypath
    ){
        //通过门牌号查找房屋id
//        Room rm = mphFindRoom(roomnumber);
        System.out.println("YeWuController.syq-----"+roomid);
        //所有权表
        Ownership ownership = new Ownership();
        ownership.setRegistertime(new Date());//登记时间
        ownership.setServicenumber(servicenumber);//业务宗号
        ownership.setRoomid(roomid);
        ownership.setOsid(osid);
        //房屋表
//        Room room = new Room();
//        room.setRoomnumber(roomnumber);
//        room.setRoomstatusid(roomstatusid);
//        room.setBuildingid(buildingid);
        //楼栋表
//        Building bu = new Building();
//        bu.setBuildingname(buildingname);
//        bu.setBuildingnumber(buildingnumber);
//        bu.setFloor(floor);
//        bu.setCount(count);
//        bu.setTotalarea(totalarea);
        //共有人
        Serviceshare serviceshare =new Serviceshare();
        if(clientid!=null){
            serviceshare.setServiceid(serviceid);
            serviceshare.setClientid(clientid);
            serviceshare.setShare(share);
            serviceshare.setSharetypeid(sharetypeid);
        }

        //业务数据封装成对象
        Service ser =new Service();
        ser.setServiceid(serviceid);
        //文件上传保存
        try {
            // FileUtil.uploadFile(application.toString().getBytes(),applicationpath,serviceid+"sqs");
            ser.setApplication(applicationpath+serviceid+"sqs");
            // FileUtil.uploadFile(idcopy.toString().getBytes(),idcopypath,serviceid+"sfz");
            ser.setIdcopy(idcopypath+serviceid+"sfz");
            // FileUtil.uploadFile(photo.toString().getBytes(),photopath,serviceid+"zp");
            ser.setPhoto(photopath+serviceid+"zp");
            //   FileUtil.uploadFile(purchasecopy.toString().getBytes(),purchasecopypath,serviceid+"fp");
            ser.setPurchasecopy(purchasecopypath+serviceid+"fp");
            //  FileUtil.uploadFile(contractcopy.toString().getBytes(),contractcopypath,serviceid+"ht");
            ser.setContractcopy(contractcopypath+serviceid+"ht");
            //   FileUtil.uploadFile(taxcopy.toString().getBytes(),taxcopypath,serviceid+"sz");
            ser.setTaxcopy(taxcopypath+serviceid+"sz");
        } catch (Exception e) {
            e.printStackTrace();
        }

      return   yeWuServiseservise.syqLuRu(ownership,ser);
    }
    @PostMapping("/tqxxf")
    @ApiOperation(value = "添加 他权项 现房 业务的方法", response = Map.class)
    public AjaxResponse tqxxf(
            @ApiParam("业务id") Integer serviceid,
            @ApiParam("一般抵押") BigDecimal generalmortgage,
            @ApiParam("最高抵押") BigDecimal maxmortgage,
            @ApiParam("抵押权人(外键)") Integer mortgagee,
            @ApiParam("抵押人(外键)") Integer mortgager,
            @ApiParam("债务人(外键)") Integer debtor,
            @ApiParam("最高债权数额")BigDecimal maxclaim,
            @ApiParam("担保范围")String scope,
            @ApiParam("债务履行期限") Date returntime,
            @ApiParam("房屋id") Integer roomid,
            @ApiParam("id")Integer emid,

            // @ApiParam("登记申请书") File application,
            @ApiParam("登记申请书存储地址") String applicationpath,
            // @ApiParam("身份证复印件") File idcopy,
            @ApiParam("身份证复印件存储地址") String idcopypath,
            // @ApiParam("一寸照片") File photo,
            @ApiParam("一寸照片存储地址") String photopath,
            //@ApiParam("购房发票复印件") File purchasecopy,
            @ApiParam("购房发票复印件存储地址") String purchasecopypath,
            //@ApiParam("购房合同复印件") File contractcopy,
            @ApiParam("购房合同复印件存储地址") String contractcopypath,
            //@ApiParam("税证复印件") File taxcopy,
            @ApiParam("税证复印件存储地址") String taxcopypath
            ){

        //数据封装成对象
        Existmortgage ex =new Existmortgage();
        ex.setServiceid(serviceid);
        ex.setGeneralmortgage(generalmortgage);
        ex.setMaxmortgage(maxmortgage);
        ex.setMortgagee(mortgagee);
        ex.setMortgager(mortgager);
        ex.setDebtor(debtor);
        ex.setMaxclaim(maxclaim);
        ex.setScope(scope);
        ex.setReturntime(returntime);
        ex.setRegistertime(new Date());
        ex.setRoomid(roomid);
        ex.setEmid(emid);
        //业务数据封装成对象
        Service ser =new Service();
        ser.setServiceid(serviceid);
        //文件上传保存
        try {
            // FileUtil.uploadFile(application.toString().getBytes(),applicationpath,serviceid+"sqs");
            ser.setApplication(applicationpath+serviceid+"sqs");
            // FileUtil.uploadFile(idcopy.toString().getBytes(),idcopypath,serviceid+"sfz");
            ser.setIdcopy(idcopypath+serviceid+"sfz");
            // FileUtil.uploadFile(photo.toString().getBytes(),photopath,serviceid+"zp");
            ser.setPhoto(photopath+serviceid+"zp");
            //   FileUtil.uploadFile(purchasecopy.toString().getBytes(),purchasecopypath,serviceid+"fp");
            ser.setPurchasecopy(purchasecopypath+serviceid+"fp");
            //  FileUtil.uploadFile(contractcopy.toString().getBytes(),contractcopypath,serviceid+"ht");
            ser.setContractcopy(contractcopypath+serviceid+"ht");
            //   FileUtil.uploadFile(taxcopy.toString().getBytes(),taxcopypath,serviceid+"sz");
            ser.setTaxcopy(taxcopypath+serviceid+"sz");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return  yeWuServiseservise.txqxfLuRu(ex,ser);
    }
    @PostMapping("/tqxzj")
    @ApiOperation(value = "添加 他权项 在建 业务的方法", response = Map.class)
    public AjaxResponse tqxxf(
            @ApiParam("业务id") Integer serviceid,
            @ApiParam("一般抵押") BigDecimal generalmortgage,
            @ApiParam("最高抵押") BigDecimal maxmortgage,
            @ApiParam("抵押权人(外键)") Integer mortgagee,
            @ApiParam("抵押人(外键)") Integer mortgager,
            @ApiParam("在建工程坐落") String location,
            @ApiParam("担保范围")String scope,
            @ApiParam("最高债权数额")BigDecimal maxclaim,
            @ApiParam("债务履行期限") Date returntime,
            @ApiParam("抵押登记证明号") String identificationnumber,
            @ApiParam("最高债权确定事实和数额") String maxfactr,
            @ApiParam("房屋id") Integer roomid,
            @ApiParam("id") Integer bmid,
            // @ApiParam("登记申请书") File application,
            @ApiParam("登记申请书存储地址") String applicationpath,
            // @ApiParam("身份证复印件") File idcopy,
            @ApiParam("身份证复印件存储地址") String idcopypath,
            // @ApiParam("一寸照片") File photo,
            @ApiParam("一寸照片存储地址") String photopath,
            //@ApiParam("购房发票复印件") File purchasecopy,
            @ApiParam("购房发票复印件存储地址") String purchasecopypath,
            //@ApiParam("购房合同复印件") File contractcopy,
            @ApiParam("购房合同复印件存储地址") String contractcopypath,
            //@ApiParam("税证复印件") File taxcopy,
            @ApiParam("税证复印件存储地址") String taxcopypath
    ){
        Beinmortgage be =new Beinmortgage();
        be.setServiceid(serviceid);
        be.setGeneralmortgage(generalmortgage);
        be.setMaxmortgage(maxmortgage);
        be.setMortgagee(mortgagee);
        be.setMortgager(mortgager);
        be.setLocation(location);
        be.setScope(scope);
        be.setMaxclaim(maxclaim);
        be.setReturntime(returntime);
        be.setIdentificationnumber(identificationnumber);
        be.setMaxfact(maxfactr);
        be.setRoomid(roomid);
        be.setBmid(bmid);
        //业务数据封装成对象
        Service ser =new Service();
        ser.setServiceid(serviceid);
        //文件上传保存
        try {
            // FileUtil.uploadFile(application.toString().getBytes(),applicationpath,serviceid+"sqs");
            ser.setApplication(applicationpath+serviceid+"sqs");
            // FileUtil.uploadFile(idcopy.toString().getBytes(),idcopypath,serviceid+"sfz");
            ser.setIdcopy(idcopypath+serviceid+"sfz");
            // FileUtil.uploadFile(photo.toString().getBytes(),photopath,serviceid+"zp");
            ser.setPhoto(photopath+serviceid+"zp");
            //   FileUtil.uploadFile(purchasecopy.toString().getBytes(),purchasecopypath,serviceid+"fp");
            ser.setPurchasecopy(purchasecopypath+serviceid+"fp");
            //  FileUtil.uploadFile(contractcopy.toString().getBytes(),contractcopypath,serviceid+"ht");
            ser.setContractcopy(contractcopypath+serviceid+"ht");
            //   FileUtil.uploadFile(taxcopy.toString().getBytes(),taxcopypath,serviceid+"sz");
            ser.setTaxcopy(taxcopypath+serviceid+"sz");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  yeWuServiseservise.txzjfLuRu(be,ser);
    }

    @PostMapping("/cf")
    @ApiOperation(value = "添加 查封 业务的方法", response = Map.class)
    public AjaxResponse cf(
            @ApiParam("业务id") Integer serviceid,
            @ApiParam("查封机关") Integer closeofficeid,
            @ApiParam("查封文件") String closefile,
            @ApiParam("查封文号") String closefilenumber,
            @ApiParam("查封时间") Date closetime,
            @ApiParam("查封期限") Date closedate,
            @ApiParam("门牌号") String roomnumber,
            @ApiParam("封id")Integer id,

         //   @ApiParam("登记申请书") File application,
            @ApiParam("登记申请书存储地址") String applicationpath
    ){
        Closeregistration clo = new Closeregistration();
        clo.setServiceid(serviceid);
        clo.setCloseofficeid(closeofficeid);
        clo.setClosefile(closefile);//查封文件
        clo.setClosefilenumber(closefilenumber);
        clo.setClosetime(closetime);
        clo.setClosedate(closedate);
        clo.setRegistertime(new Date());
        clo.setDescript(roomnumber);
        clo.setId(id);
        //业务数据封装成对象
        Service ser =new Service();
        try {
        //    FileUtil.uploadFile(application.toString().getBytes(),applicationpath,serviceid+"sqs");
            ser.setApplication(applicationpath+serviceid+"sqs");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return yeWuServiseservise.cfLuru(clo,ser);
    }
    @PostMapping("/yg")
    @ApiOperation(value = "添加 预告 业务的方法(仅添加登记申请书，身份证、税证复印件,一寸照片在此处用于存放roomid)", response = Map.class)
    public AjaxResponse yg(
            @ApiParam("业务id") Integer serviceid,
            @ApiParam("申请人") Integer proposer,
            @ApiParam("代理人") Integer  agent,
            @ApiParam("申请人身份证") String procard,
            @ApiParam("代理人身份证") String agecard,
          //  @ApiParam("登记申请书") File application,
            @ApiParam("登记申请书存储地址") String applicationpath,
         //   @ApiParam("身份证复印件") File idcopy,
            @ApiParam("身份证复印件存储地址") String idcopypath,
          //  @ApiParam("税证复印件") File taxcopy,
            @ApiParam("税证复印件存储地址") String taxcopypath,
            @ApiParam("一寸照片 / 此处存放的是roomid") String photo

    ){
        System.out.println("YeWuController.yg"+serviceid+"|"+proposer+"|"+agent+"|"+procard+"|"+agecard+"|");
        //业务数据封装成对象
        Service ser =new Service();
        ser.setServiceid(serviceid);
        ser.setPhoto(photo);
        ser.setProposer(proposer);
        ser.setAgent(agent);
        //文件上传保存
        try {
         //   FileUtil.uploadFile(application.toString().getBytes(),applicationpath,serviceid+"sqs");
            ser.setApplication(applicationpath+serviceid+"sqs");
         //   FileUtil.uploadFile(idcopy.toString().getBytes(),idcopypath,serviceid+"sfz");
            ser.setIdcopy(idcopypath+serviceid+"sfz");
         //   FileUtil.uploadFile(taxcopy.toString().getBytes(),taxcopypath,serviceid+"sz");
            ser.setTaxcopy(taxcopypath+serviceid+"sz");
        } catch (Exception e) {
            e.printStackTrace();
        }


        return yeWuServiseservise.ygLuRu(ser,procard,agecard);
    }
    @PostMapping("/jf")
    @ApiOperation(value = "添加 解除查封 业务的方法", response = Map.class)
    public AjaxResponse jf(
            @ApiParam("业务id") Integer serviceid,
            @ApiParam("解除查封文件") String openfile,
            @ApiParam("解除查封文号") String openfilenumber,
            @ApiParam("解除查封时间") Date opentime,
            @ApiParam("门牌号") String roomnumber,
            @ApiParam("封id")Integer id,
            // @ApiParam("登记申请书") File application,
            @ApiParam("登记申请书存储地址") String applicationpath,
            // @ApiParam("身份证复印件") File idcopy,
            @ApiParam("身份证复印件存储地址") String idcopypath,
            // @ApiParam("一寸照片") File photo,
            @ApiParam("一寸照片存储地址") String photopath,
            //@ApiParam("购房发票复印件") File purchasecopy,
            @ApiParam("购房发票复印件存储地址") String purchasecopypath,
            //@ApiParam("购房合同复印件") File contractcopy,
            @ApiParam("购房合同复印件存储地址") String contractcopypath,
            //@ApiParam("税证复印件") File taxcopy,
            @ApiParam("税证复印件存储地址") String taxcopypath
    ){
            Closeregistration clo = new Closeregistration();
            clo.setOpentime(opentime);
            clo.setOpenregistertime(new Date());
            clo.setDescript(roomnumber);
            clo.setOpenfile(openfile);
            clo.setOpenfilenumber(openfilenumber);
            clo.setId(id);
            //业务数据封装成对象
            Service ser = new Service();
            ser.setServiceid(serviceid);
        //文件上传保存
        try {
            // FileUtil.uploadFile(application.toString().getBytes(),applicationpath,serviceid+"sqs");
            ser.setApplication(applicationpath+serviceid+"sqs");
            // FileUtil.uploadFile(idcopy.toString().getBytes(),idcopypath,serviceid+"sfz");
            ser.setIdcopy(idcopypath+serviceid+"sfz");
            // FileUtil.uploadFile(photo.toString().getBytes(),photopath,serviceid+"zp");
            ser.setPhoto(photopath+serviceid+"zp");
            //   FileUtil.uploadFile(purchasecopy.toString().getBytes(),purchasecopypath,serviceid+"fp");
            ser.setPurchasecopy(purchasecopypath+serviceid+"fp");
            //  FileUtil.uploadFile(contractcopy.toString().getBytes(),contractcopypath,serviceid+"ht");
            ser.setContractcopy(contractcopypath+serviceid+"ht");
            //   FileUtil.uploadFile(taxcopy.toString().getBytes(),taxcopypath,serviceid+"sz");
            ser.setTaxcopy(taxcopypath+serviceid+"sz");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return yeWuServiseservise.jfLuru(clo,ser);
    }
}
